package com.oro.backingbeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.oro.domain.Email;
import com.oro.domain.IndirizzoAU;
import com.oro.domain.MigrationAgent;
import com.oro.domain.Telephone;
import com.oro.service.StudentService;

public class InserisciMigrationAgentBean implements Serializable, ActionListener {

	private static final Logger logger = Logger
			.getLogger(InserisciUtenteBean.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 752827613687476309L;

	private List<SelectItem> emailTypeOptions;

	private MigrationAgent migrationAgentInput;
	private MigrationAgent migrationAgentOutput;
	private List<SelectItem> phoneCountryOptions;
	private List<SelectItem> phoneTypeOptions;
	private List<SelectItem> stateOptions;
	@Autowired
	private StudentService studentService;

	private List<String> townsAu;

	private List<String> townsIt;

	public InserisciMigrationAgentBean() {

	}

	public List<String> completeTownAu(String value) {
		List<String> result = new ArrayList<String>();
		for (String town : townsAu) {
			if (town != null)
				if (town.startsWith(value))
					result.add(town);
		}
		return result;
	}

	public List<String> completeTownIt(String value) {
		List<String> result = new ArrayList<String>();
		for (String town : townsIt) {
			if (town != null)
				if (town.startsWith(value))
					result.add(town);
		}
		return result;
	}

	public List<SelectItem> getEmailTypeOptions() {
		return emailTypeOptions;
	}

	private Logger getLogger() {
		return logger;
	}

	public MigrationAgent getMigrationAgentInput() {
		return migrationAgentInput;
	}

	public MigrationAgent getMigrationAgentOutput() {
		return migrationAgentOutput;
	}

	public List<SelectItem> getPhoneCountryOptions() {
		return phoneCountryOptions;
	}

	public List<SelectItem> getPhoneTypeOptions() {
		return phoneTypeOptions;
	}

	public List<SelectItem> getStateOptions() {
		return stateOptions;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public List<String> getTownsAu() {
		return townsAu;
	}

	public List<String> getTownsIt() {
		return townsIt;
	}

	@PostConstruct
	public void postConstruct() {
		migrationAgentInput = new MigrationAgent();
		migrationAgentOutput = new MigrationAgent();
		townsAu = studentService.getTownsAu();
		townsIt = studentService.getTownsIt();
		emailTypeOptions = new ArrayList<SelectItem>();
		for (String type : Email.getTypes()) {
			emailTypeOptions.add(new SelectItem(type, type));
		}
		phoneTypeOptions = new ArrayList<SelectItem>();
		for (String type : Telephone.getTypes()) {
			phoneTypeOptions.add(new SelectItem(type, type));
		}
		phoneCountryOptions = new ArrayList<SelectItem>();
		for (String country : Telephone.getCountries()) {
			phoneCountryOptions.add(new SelectItem(country, country));
		}
		stateOptions = new ArrayList<SelectItem>();
		for (String state : IndirizzoAU.getAustralianStates()) {
			stateOptions.add(new SelectItem(state, state));
		}
		getLogger().info("inserisciMigrationAgentBean has been constructed.");
	}

	@PreDestroy
	public void preDestroy() {
		getLogger().info("inserisciMigrationAgentBean is being destroyed.");
	}

	public void reset(ActionEvent e) {
		postConstruct();
	}
	
	public void reset() {
		postConstruct();
	}

	public void setEmailTypeOptions(List<SelectItem> emailTypeOptions) {
		this.emailTypeOptions = emailTypeOptions;
	}

	public void setMigrationAgentInput(MigrationAgent migrationAgentInput) {
		this.migrationAgentInput = migrationAgentInput;
	}

	public void setMigrationAgentOutput(MigrationAgent migrationAgentOutput) {
		this.migrationAgentOutput = migrationAgentOutput;
	}

	public void setPhoneCountryOptions(List<SelectItem> phoneCountryOptions) {
		this.phoneCountryOptions = phoneCountryOptions;
	}

	public void setPhoneTypeOptions(List<SelectItem> phoneTypeOptions) {
		this.phoneTypeOptions = phoneTypeOptions;
	}

	public void setStateOptions(List<SelectItem> stateOptions) {
		this.stateOptions = stateOptions;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public void setTownsAu(List<String> townsAu) {
		this.townsAu = townsAu;
	}

	public void setTownsIt(List<String> townsIt) {
		this.townsIt = townsIt;
	}

	public String submit() {

		try {
			studentService.save(migrationAgentInput);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		
		migrationAgentOutput = migrationAgentInput;
		migrationAgentInput = new MigrationAgent();

		return "success";
	}

	@Override
	public void processAction(ActionEvent arg0) throws AbortProcessingException {
		// TODO Auto-generated method stub
		
	}

}
